﻿using Elastic.Apm;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading;

namespace Elastic_EF.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class MaiPointerController : ControllerBase
    {
        [HttpGet]
        public void RecordMyApmData()

        {
            var transaction = Agent.Tracer.CurrentTransaction;

            var span1 = transaction.StartSpan("Stage 1", "Customize");

            Thread.Sleep(300);

            span1.End();

            Thread.Sleep(200);

            var span2 = transaction.StartSpan("Stage 2", "Customize");

            Thread.Sleep(100);

            span2.End();

            Thread.Sleep(100);

            var span3 = transaction.StartSpan("Stage 3", "Customize");

            Thread.Sleep(500);

            span3.End();
        }

        [HttpGet("Exception")]
        public void TestException()
        {
            try
            {
                //int a = 235;
                //int b = 0;
                //var c = a / b;
                throw new Exception("捕获的异常 ");
            }
            catch (Exception ex)
            {
                // ignored
                Console.WriteLine($"异常信息：{ex.Message}");
            }
            throw new Exception("未捕获的异常");
        }
    }
}